/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author ITI
 */
public class SearchServlet extends HttpServlet {
    ResultSet rs ;
 
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

                
        String productname = request.getParameter("check_for");
           PrintWriter out = response.getWriter();
          try{
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/Shopping", "root", "asmaaelmasry");
            PreparedStatement ps =  (PreparedStatement) con.prepareStatement("select * from Product where name like '%"+productname+"%'");
            rs = ps.executeQuery();
            while(rs.next())
            {
            
                out.println("<a href=singleproduct.jsp?prodId="+rs.getInt("id")+">"+rs.getString("name")+"</a>");
                out.println("</br>");
              
            
            
            }
          }
          catch(ClassNotFoundException x){x.printStackTrace();}
          catch(SQLException ex){ex.printStackTrace();}

        
        
        
    }

}
